-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow customer returns to reference existing ReturnItem
s on create through API
#4007
Allow customer returns to reference existing ReturnItem
s on create through API
#4007
Conversation
api/spec/requests/spree/api/customer_returns_controller_spec.rb
Outdated
Show resolved
Hide resolved
api/spec/requests/spree/api/customer_returns_controller_spec.rb
Outdated
Show resolved
Hide resolved
34db039
to
8ba34ba
Compare
@forkata Thanks for the PR! For anyone else reviewing, here's a handy link to the backend counterpart for this feature.
👍
Do you think there's a way to deprecate passing the hash, just to be sure we are not breaking existing applications? I'm afraid that a lot of people are looking at the source code to understand how to use our APIs, also because at the moment the doc is not super reliable as you saw.
I'd avoid adding this validation, maybe just because I don't understand why a |
Thank you for your quick feedback @kennyadsl!
That is a great suggestion! I was contemplating handling both formats to ensure backwards compatibility. What I can do is add a change and corresponding tests to handle
I was also unsure if this was a |
8ba34ba
to
0de072f
Compare
0de072f
to
058b856
Compare
Hi @kennyadsl,
|
2d7079c
to
a3031d6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Going to merge this after we release 3.0 (this will be present in 3.1).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@forkata thanks for this fix, I really appreciated being guided in the review by the very detailed commits ❤️
I left a couple of comments, let me know what you think!
return_item.assign_attributes(item_params) | ||
|
||
return_item | ||
end.compact |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why we need compact
after the map
cycle, I suppose we're always returning a Spree::ReturnItem
with each iteration, and when a return item cannot be found from item_params[:id]
an exception is raised... but maybe I'm overlooking some scenario?
Also, I think the naming of the method reflects only part of what this method does here. What about a more general build_customer_return
, since it's actually building the instance stored in@customer_return
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why we need compact after the map cycle
I agree, upon closer inspection I don't think the .compact
is necessary here.
I think the naming of the method reflects only part of what this method does here.
That's a great point, I think build_customer_return
is a better name here, I will update that!
Thanks for the feedback, I will update this change to reflect the above suggestions!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spaghetticode I've addressed your comments here and this should be ready for another review 🙏🏼
In a subsequent change we are going to be adding a parallel context for when we want to reference existing return items, so this is just the first step to that change. There should be no change in functionality just spacing here. Co-authored-by: Mike Conlin <mike@super.gd>
This change adds a pending test which demonstrates an issue with the `create` action when attempting to pass a reference to existing return items from a previously created return authorization. Rails does not allow us to update the association on the return items to the customer return while creating the return. Co-authored-by: Mike Conlin <mike@super.gd>
This change pulls the `before_action` from the backend customer returns controller for parsing `return_items_attributes` in order to handle creating a new customer return which references existing return items from a return authorization. This change works around a limitation in Rails when trying to update the association on existing nested resource while creating the related record. Co-authored-by: Mike Conlin <mike@super.gd>
This is a behaviour which was previously undocumented worked because of the native Rails parameter parsing for nested attributes. This is not something we want to support through the API going forward so we are adding a deprecation warning for anyone using this behaviour currently.
a3031d6
to
00ffaf2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@forkata thank you, LGTM 👍
Description
This change fixes an issue in the API customer returns controller
create
action when referencing existing return items from a return authorization. This works onupdate
but Rails has a limitation where it cannot set thecustomer_return_id
on theReturnItem
records when they are passed as nested attributes, because that record doesn't exist. This will fail with anActiveRecord::RecordNotFound
error. This behaviour is documented, though slightly buried deep in the documentation here.We already allow this in the
CustomerReturnsController
in the backend, however it requires a bit of parameter mangling to get the exact behaviour we want. In this change we have pulled thebuild_return_items_from_params
before action from the backend controller and modified it to better fit the API use case.The main changes required to the
before_action
were toreturned
flag, which is specific to the backend form.return_items_attributes
to be passed as a hash of hashes (default for nested forms) as well as anArray
. I've also added a deprecation when providing these as a hash of hashes.reception_status_event
is not provided, this is not required by the API controller, but required in the backend.Checklist:
I have attached screenshots to this PR for visual changes (if needed)